package thread;

import lombok.SneakyThrows;

import java.util.concurrent.TimeUnit;

/**
 * @author ZhuQi
 * @ClassName ThreadStopDemo2
 * @Date 2022/9/13 9:43
 * @Version 1.0
 * @Description 测试线程中断
 */
public class ThreadStopDemo2 {

    @SneakyThrows
    public static void main(String[] args) {
        Thread thread1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("线程停止");
                }
                System.out.println("继续执行");
                System.out.println(Thread.currentThread().isInterrupted());
//                try {
//                    TimeUnit.SECONDS.sleep(1);
//                } catch (InterruptedException e) {
//                    System.out.println(e.getMessage());
//                    System.out.println("线程中断");
//
//                    System.out.println(Thread.currentThread().isInterrupted());
//                    Thread.currentThread().interrupt();
//                }
            }
        });
        thread1.start();

        TimeUnit.SECONDS.sleep(3);

        new Thread(thread1::interrupt).start();

        new Thread(thread1::interrupt).start();
    }
}
